import Vue from 'vue';
import Axios from 'axios';
import Qs from 'qs';
import VModal from 'vue-js-modal';
import paging from '../components/pageing/paging.js';
import '../basic/basic.css';
Vue.use(VModal,{dialog:true});

new Vue({
    el:'#louie-list',
    data:{
        //--公共--
        url:$('#louie-list').attr('data-url'),
        //--提现状态导航--
        nav:[
            {name:'待提现',status:0,active:true},
            {name:'已提现',status:1,active:false},
            {name:'拒绝提现',status:2,active:false}
        ],
        nav_index:0,  //导航下标
        activity_id:0,  //活动id
        //--分页--
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        total:0,
        //--数据--
        list:[],
        //--提现、删除、拒绝状态--
        disabled:false,
        //批量提现操作
        all_select:false
    },
    components:{
        paging:paging
    },
    created:function(){
        var hash = this.decodeHash();
        if(hash.activity_id) this.activity_id = hash.activity_id;
        this.getTotal();
        this.li();
    },
    methods:{
        //点击导航
        changeNav:function(index){
            if(this.nav_index != index){
                this.nav[this.nav_index].active = false;
                this.nav_index = index;
                this.nav[index].active = true;
                this.pageNum = 1;
                this.getTotal();
                this.li();
            }
        },
        //点击分页
        changePage:function(page){
            this.pageNum = page;
            this.li();
        },
        //获取总数
        getTotal:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'exchange',
                doing:'total',
                status:this.nav[this.nav_index].status,
                activity_id:this.activity_id
            }))
                .then((res)=>{
                    this.total = parseInt(res.data.data);
                })
                .catch(error=>console.log(error));
        },
        //通过提现
        pass:function(index){
            var self = this;
            this.confirm('确定要处理该提现吗？',function(is_true){
                if(is_true){
                    self.disabled = true;
                    Axios.post(self.url,Qs.stringify({
                        pages:'exchange',
                        doing:'deposit',
                        deposit_id:self.list[index].id
                    }))
                        .then((res)=>{
                            var data = res.data;
                            if(data.status == 'success'){
                                self.getTotal();
                                self.li();
                            }else{
                                self.alert(data.err_msg);
                            }
                            self.disabled = false;
                        })
                        .catch(error=>console.log(error));
                }
            });
        },
        //拒绝提现
        refuse:function(index){
            var self = this;
            this.confirm('确定拒绝提现申请吗？',function(is_true){
                if(is_true){
                    self.disabled = true;
                    Axios.post(self.url,Qs.stringify({
                        pages:'exchange',
                        doing:'refuse',
                        deposit_id:self.list[index].id
                    }))
                        .then((res)=>{
                            if(res.data.status == 'success'){
                                self.getTotal();
                                self.li();
                            }else{
                                self.alert(res.data.err_msg);
                            }
                            self.disabled = false;
                        })
                        .catch(error=>console.log(error));
                }
            });
        },
        //删除提现
        del:function(index){
            var self = this;
            this.confirm('确定要删除吗？',function(is_true){
                if(is_true){
                    self.disabled = true;
                    Axios.post(self.url,Qs.stringify({
                        pages:'exchange',
                        doing:'delete',
                        deposit_id:self.list[index].id
                    }))
                        .then((res)=>{
                            if(res.data.status == 'success'){
                                self.getTotal();
                                self.li();
                            }else{
                                self.alert(res.data.err_msg);
                            }
                            self.disabled = false;
                        })
                        .catch(error=>console.log(error));
                }
            })
        },
        //获取list
        li:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'exchange',
                doing:'li',
                status:this.nav[this.nav_index].status,
                activity_id:this.activity_id,
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum]
            }))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.list = res.data.data;
                    }else{
                        this.list = [];
                    }
                })
                .catch(error=>console.log(error));
        },
        //批量选择操作
        allSelectChange(){
            var deposits = this.$refs.allDeposit.deposits;
            if(this.all_select){
                for(var i=0;i<deposits.length;i++){
                    deposits[i].checked = false;
                }
                this.all_select = false;
            }else{
                for(var i=0;i<deposits.length;i++){
                    deposits[i].checked = true;
                }
                this.all_select = true;
            }
        },
        //批量提现
        allDeposit:function(){
            var deposits = this.$refs.allDeposit.deposits;
            var checked = [];
            for(var i=0;i<deposits.length;i++){
                if(deposits[i].checked){
                    checked.push(deposits[i].value);
                }
            }
            if(checked.length <= 0){
                this.alert('至少选择一个提现请求');
                return false;
            }
            Axios.post(this.url,Qs.stringify({
                pages:'exchange',
                doing:'allDeposit',
                data:checked
            }))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.alert(res.data.data);
                    }else{
                        this.alert('提现失败');
                    }
                    this.getTotal();
                    this.li();
                })
                .catch(error=>console.log(error));
        },
        //alert
        alert:function(text){
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {title:'关闭'}
                ]
            });
        },
        //confirm
        confirm:function(text,call){
            var self = this;
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {
                        title: '取消',
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(false);
                        }
                    },
                    {
                        title: '确认',
                        default: true,
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(true);
                        }
                    }
                ]
            });
        },
        //解析链接哈希
        decodeHash:function(){
            var hash = window.location.hash;
            hash = hash.substring(1);
            if(hash == '') return {};
            var tempArr = hash.split('#');
            var data = {};
            for(var i=0;i<tempArr.length;i++){
                var tArr = tempArr[i].split('=');
                data[tArr[0]] = tArr[1];
            }
            return data;
        }
    }
})